package com.iRomantic.controller.manager;

import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iRomantic.common.BaseController;
import com.iRomantic.common.oss.OSSService;
import com.iRomantic.common.util.FileUtil;
import com.iRomantic.config.Constant;
import com.iRomantic.domain.entity.Wedding;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;

public class WeddingController extends BaseController{
	
	private static final Logger logger = LoggerFactory.getLogger(WeddingController.class);
	
	/**
	 * 创建婚礼
	 * @throws Exception 
	 */
	public void create() throws Exception{
		String path = this.getSession().getServletContext().getRealPath(Constant.TEMP_FOLDER);
		
		UploadFile bridegroomPic = this.getFile("wedding.bridegroomPic",path);
		UploadFile bridePic = this.getFile("wedding.bridePic",path);
		Wedding wedding = this.getModel(Wedding.class);
		wedding.set("id", UUID.randomUUID().toString());
		
		//新郎图片处理
		if(bridegroomPic!=null){
			String bridegroomName = FileUtil.randomFileName(bridegroomPic.getFileName());
			//bridegroomPic.getFile().renameTo(new File(path,bridegroomName));
			wedding.set("bridegroomUrl", bridegroomName);
			//InputStream input = new FileInputStream(bridegroomPic.getFile());
			OSSService.getInstance().save(Constant.WEDDING_PICTURE+wedding.getStr("id")+"/"+wedding.getStr("bridegroomUrl"), bridegroomPic.getFile());
			
			bridegroomPic.getFile().delete();
		}
		
		//新娘图片处理
		if(bridePic!=null){
			String bridePicName = FileUtil.randomFileName(bridePic.getFileName());
			//bridePic.getFile().renameTo(new File(path,bridePicName));
			wedding.set("brideUrl", bridePicName);
			OSSService.getInstance().save(Constant.WEDDING_PICTURE+wedding.getStr("id")+"/"+wedding.getStr("brideUrl"), bridePic.getFile());
			bridePic.getFile().delete();
		}
		wedding.save();
		this.renderJson(wedding);
	}
	
	/**
	 * 列表
	 */
	public void list(){
		Page<Wedding> page = Wedding.DAO.paginate(1, 10, " select * ", " from t_Wedding ");
		this.setAttr("page", page);
		this.renderWebJsp("manager/wedding/list.jsp");
	}
	
	
	public void view(){
		String weddingId = this.getPara();
		Wedding wedding = Wedding.DAO.findById(weddingId);
		this.setAttr("wedding", wedding);
		this.setAttr("hotel", wedding.getHotel());
		this.setAttr("enterprise", wedding.getEnterprise());
		this.renderWebJsp("manager/wedding/view.jsp");
	}

}
